package com.elitesland.core.base;

import com.elitesland.core.config.CustomAuditingListener;
import com.elitesland.system.annotation.Comment;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * <pre>
 *     JPA数据实体类的基础类，用于唯一ID生成策略绑定
 *     审计字段、版本和逻辑删除标记，
 *     未来扩展租户ID信息
 * </pre>
 *
 * @author Moz
 * @date 3/17/2020
 */
@Data
@EntityListeners(value = {CustomAuditingListener.class})
@MappedSuperclass
public class BaseModel {

    @Id
    @GenericGenerator(name = "el-id", strategy = "com.elitesland.core.util.IdGenerator")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "el-id")
    @Comment("记录唯一ID")
    @Column(name = "id")
    private Long id;

    @Comment(value = "租户ID")
    @Column(name = "tenant_id")
    private Long tenantId;

    @Comment("备注")
    @Column(name = "remark")
    private String remark;

    @Comment("记录创建者ID")
    @Column(name = "create_user_id", updatable = false)
    private Long createUserId;

    @CreatedBy
    @Comment("记录创建者")
    @Column(name = "creator", columnDefinition = "varchar(256)")
    private String creator;

//    @CreatedDate
    @Comment("记录创建时间")
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    @Comment("记录最后更新者ID")
    @Column(name = "modify_user_id")
    private Long modifyUserId;

//    @LastModifiedBy
    @Comment("记录最后更新者")
    @Column(name = "updater", columnDefinition = "varchar(256)")
    private String updater;

//    @LastModifiedDate
    @Comment("记录最后更新时间")
    @Column(name = "modify_time")
    private LocalDateTime modifyTime;

    @Comment("逻辑删除，0：未删除，1：已删除")
    @Column(name = "delete_flag", columnDefinition = "int(2) default 0")
    private Integer deleteFlag;

    @Comment("锁版本")
    @Column(name = "audit_data_version", columnDefinition = "int(4) default 0")
    private Integer auditDataVersion;
}
